package com.salary.snippet

import scala.xml.{NodeSeq,Text}
import net.liftweb.http.{FileParamHolder,S,SHtml,StatefulSnippet, RequestVar}
import net.liftweb.common.{Box,Empty,Full}
import net.liftweb.util.Helpers.{bind,chooseTemplate}
import net.liftweb.util.Helpers._
import net.liftweb.mapper._
import java.io.File
import java.io.FileOutputStream;
import java.io.FileInputStream

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFDataFormatter
import org.apache.poi.ss.usermodel.Cell
import snippet._
import model.SocialSecurity

class SocialSecuritys extends TableComm{
	def upload(xhtml : NodeSeq) : NodeSeq = {
		uploadBase(xhtml)
	}
	override def addItem(row: HSSFRow) = {
		val item = addOneItem(row)
		item.save
	}
	object currentItemVar extends RequestVar[SocialSecurity]({
		SocialSecurity.create
	})
	def currentItem = currentItemVar.is
	override val sheetName = "杭州"
	override val firstRow = 2

	def show(xhtml : NodeSeq) : NodeSeq = {
		def getDB(sql:String):List[SocialSecurity] = {
			if (sql == "") 		SocialSecurity.findAll()
			else SocialSecurity.findAll(BySql(sql, null))
		}
		val db:List[SocialSecurity] = getDB(currentSql.is)
		val sn = getSn(currentSn.is, db.length)
	
		var pages = (sn + numOfPage)/numOfPage
		var sql = currentSql.is
		
		def doGoPage() {
			def getPages() {
				if (pages <= 0) 1
				else pages
			}
			S.redirectTo("/SocialSecuritysView", ()=>saveCurrenVal((pages-1)*numOfPage, sql))
		}
		
		def doFilter() {
			S.redirectTo("/SocialSecuritysView", ()=>saveCurrenVal(0, sql))
		}
		bind("info", xhtml,
			"sql" -> SHtml.text(sql, sql = _)%("size" -> "80"),
			"sqlfilter" -> SHtml.submit("Filter", doFilter),
			
			"table" -> buildEntry(xhtml, sn, db),
			
			"prev" -> { SHtml.link("/SocialSecuritysView", () => saveCurrenVal(sn - numOfPage, sql), Text("Prev"))},
			"next" -> { SHtml.link("/SocialSecuritysView", () => saveCurrenVal(sn + numOfPage, sql), Text("Next"))},
			
			"pagesInfo" -> Text((sn+1).toString + " to " + GetPageMaxSn(sn, db.length).toString + " of " + db.length.toString),
			"pagesNo" -> SHtml.text(pages.toString, v => pages = v.toInt)%("size" -> "3"),
			"pagesTotal" -> Text(((db.length+numOfPage-1)/numOfPage).toInt.toString),
			"pagesJump" -> SHtml.submit("Go", doGoPage)
			)
	}

	def buildEntry(xhtml : NodeSeq, sn:Int, db:List[SocialSecurity]) : NodeSeq = {
		val db1 = db.takeRight(db.length - sn).take(numOfPage)
		db1.flatMap({item => 
			bind("item", chooseTemplate("info", "table", xhtml),
			"idx" -> Text(item.idx.toString),
			"name" -> Text(item.name.toString),
			"time" -> Text(item.time.toString),
			"base" -> Text(item.base.toString),
			"SAFbyCompany" -> Text(item.SAFbyCompany.toString),
			"SAFbySelf" -> Text(item.SAFbySelf.toString),
			"UEIbyCompany" -> Text(item.UEIbyCompany.toString),
			"UEIbySelf" -> Text(item.UEIbySelf.toString),
			"MPIbyCompany" -> Text(item.MPIbyCompany.toString),
			"MPIbySelf" -> Text(item.MPIbySelf.toString),
			"BMPIbyCompany" -> Text(item.BMPIbyCompany.toString),
			"BMPIbySelf" -> Text(item.BMPIbySelf.toString),
			"IDA" -> Text(item.IDA.toString),
			"MTI" -> Text(item.MTI.toString),
			"actions" -> { SHtml.link("/SocialSecuritysView", () => item.delete_!, Text("Delete")) ++
                               SHtml.link("/SocialSecuritysEdit", () => currentItemVar(item), Text("Edit")) })
		 })
	} 
	
	def addOneItem(row: HSSFRow): SocialSecurity = {
		val item = SocialSecurity.create
		item.idx( readCell(row.getCell(1)) )
		item.name( readCell(row.getCell(2)) )
		item.time( readCell(row.getCell(5)) )
		item.base( readCellDouble(row.getCell(6)) )
		item.SAFbyCompany( readCellDouble(row.getCell(7)) )
		item.SAFbySelf( readCellDouble(row.getCell(8)) )
		item.UEIbyCompany( readCellDouble(row.getCell(10)) )
		item.UEIbySelf( readCellDouble(row.getCell(11)) )
		item.MPIbyCompany( readCellDouble(row.getCell(13)) )
		item.MPIbySelf( readCellDouble(row.getCell(14)) )
		item.BMPIbyCompany( readCellDouble(row.getCell(15)) )
		item.BMPIbySelf( readCellDouble(row.getCell(16)) )
		item.IDA( readCellDouble(row.getCell(17)) )
		item.MTI( readCellDouble(row.getCell(18)) )
		item
	}
	
	def buildEditEntry(xhtml : NodeSeq, item: SocialSecurity) : NodeSeq = {
		bind("item", chooseTemplate("info", "table", xhtml),
			"idx" -> SHtml.text(item.idx, item.idx(_)),
			"name" -> SHtml.text(item.name, item.name(_)),
			"time" -> SHtml.text(item.time, item.time(_)),
			"base" -> SHtml.text(item.base.toString, v => item.base(v.toDouble)),
			"SAFbyCompany" -> SHtml.text(item.SAFbyCompany.toString, v => item.SAFbyCompany(v.toDouble)),
			"SAFbySelf" -> SHtml.text(item.SAFbySelf.toString, v => item.SAFbySelf(v.toDouble)),
			"UEIbyCompany" -> SHtml.text(item.UEIbyCompany.toString, v => item.UEIbyCompany(v.toDouble)),
			"UEIbySelf" -> SHtml.text(item.UEIbySelf.toString, v => item.UEIbySelf(v.toDouble)),
			"MPIbyCompany" -> SHtml.text(item.MPIbyCompany.toString, v => item.MPIbyCompany(v.toDouble)),
			"MPIbySelf" -> SHtml.text(item.MPIbySelf.toString, v => item.MPIbySelf(v.toDouble)),
			"BMPIbyCompany" -> SHtml.text(item.BMPIbyCompany.toString, v => item.BMPIbyCompany(v.toDouble)),
			"BMPIbySelf" -> SHtml.text(item.BMPIbySelf.toString, v => item.BMPIbySelf(v.toDouble)),
			"IDA" -> SHtml.text(item.IDA.toString, v => item.IDA(v.toDouble)),
			"MTI" -> SHtml.text(item.MTI.toString, v => item.MTI(v.toDouble)))
	}

	def add(xhtml : NodeSeq) : NodeSeq = {
		val item = SocialSecurity.create
		def doAdd () { 
			item.save
 		    S.redirectTo("/SocialSecuritysView")
		}

		bind("info", xhtml,
			 "table" -> buildEditEntry(xhtml, item),
			 "submit" -> SHtml.submit("Add", doAdd))
	}

	def edit(xhtml : NodeSeq) : NodeSeq = {
		def doSave () = {
		  currentItem.validate match {
			case Nil =>
			  currentItemVar.save
			  S.redirectTo("/SocialSecuritysView")
			case x => S.error(x)
		  }
		}

		val item = currentItem

		bind("info", xhtml,
			 "key" -> SHtml.hidden(() => currentItemVar(item)),
			 "table" -> buildEditEntry(xhtml, item),
			 "submit" -> SHtml.submit("Save", doSave))
	}

}
